package com.smarthealthring.mm.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.clj.fastble.BleManager;
import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.base.IPresenter;
import com.smarthealthring.mm.ble.BdProtocol;
import com.smarthealthring.mm.ble.RtEvent;
import com.smarthealthring.mm.utils.ComToolBar;
import com.smarthealthring.mm.utils.RxBus;
import com.smarthealthring.mm.utils.RxBusBaseMessage;
import com.smarthealthring.mm.utils.SPUtils;
import com.smarthealthring.mm.widgets.SingleItemView;
import com.smarthealthring.mm.widgets.itemdecoration.LinearLayoutDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * 设备类型列表
 * Created by bilibili on 2018/5/15.
 */

public class DeviceInfoActivity extends BaseActivity {

    @BindView(R.id.tv_ble_mac)
    TextView mTvBleMac;
    @BindView(R.id.tv_power)
    TextView mTvPower;
    @BindView(R.id.tv_gj_ver)
    TextView mTvGjVer;
    @BindView(R.id.btn_unbond)
    Button mBtnUnBond;
    @BindView(R.id.siv_lan_set)
    SingleItemView mSivLanSet;

    @Override
    protected void initComToolBar() {
        ImageView ivLeft = ComToolBar.setleftImg(mContext, R.drawable.back_white);
        RxView.clicks(ivLeft)
                .subscribe(o -> finish());
        ComToolBar.setTitle(mContext, "魔戒");
    }

    @Override
    protected int getLayout() {
        return R.layout.act_device_info;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {

        initlanPicker();

        mTvBleMac.setText(SPUtils.getString(SPUtils.DEVICE_MAC, ""));
        mTvGjVer.setText("v" + SPUtils.getString(SPUtils.DEVICE_VERSION, ""));
        mTvPower.setText(SPUtils.getInt(SPUtils.DEVICE_POWER, 75) + "%");
        mBtnUnBond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BleManager.getInstance().isBlueEnable() && BdProtocol.IS_SYNC_DATA == 0 && BleManager.getInstance().isConnected(SPUtils.getString(SPUtils.DEVICE_MAC, ""))) {
                    BdProtocol.unBond(BdProtocol.mBleDevice);
                } else {
                    Toast.makeText(mContext, "正在同步数据中，请在同步完成后解绑", Toast.LENGTH_LONG).show();
                }
            }
        });
        mSivLanSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (BleManager.getInstance().isBlueEnable() && BdProtocol.IS_SYNC_DATA == 0 && BleManager.getInstance().isConnected(SPUtils.getString(SPUtils.DEVICE_MAC, ""))) {
                    lanPicker.show();
                } else {
                    Toast.makeText(mContext, "正在同步数据中，请在同步完成后设置语言", Toast.LENGTH_LONG).show();
                }
            }
        });

        // 默认中文
        if (SPUtils.getInt(SPUtils.DEVICE_LAN, 1) == 1) {
            mSivLanSet.setmTvRight(
                    "中文"
            );
        } else {
            mSivLanSet.setmTvRight(
                    "英文"
            );
        }

        registerEvent();

    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }

    void registerEvent() {

        RxBus.getInstance().tObservable(RtEvent.UNBOND_SUCCESS_EVENT, RxBusBaseMessage.class)
                .subscribe(new Consumer<RxBusBaseMessage>() {
                    @Override
                    public void accept(RxBusBaseMessage rxBusBaseMessage) throws Exception {
                        Log.e("reg_unbond_succ===", "in...");
                        startActivity(new Intent(DeviceInfoActivity.this, HomeActivity.class));
                    }
                });
    }

    /**
     * 语言选择器
     */
    private OptionsPickerView lanPicker;

    private void initlanPicker() {
        List<String> lanList = new ArrayList() {{
            add("中文");
            add("English");
        }};

        lanPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = lanList.get(options1);
                mSivLanSet.setmTvRight(tx);
                if (options1 == 0) {
                    SPUtils.setInt(SPUtils.DEVICE_LAN, 1);
                } else {
                    SPUtils.setInt(SPUtils.DEVICE_LAN, 0);
                }
                BdProtocol.setLan(BdProtocol.mBleDevice);
            }
        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvAdd = (TextView) v.findViewById(R.id.tv_add);

                        tvAdd.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                lanPicker.returnData();
                                lanPicker.dismiss();
                            }
                        });
                    }
                })
                .isDialog(true)
                .build();

        lanPicker.setPicker(lanList);//添加数据
    }
}
